package com.shenmazg6.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenmazg6.pojo.TbProduct;
import com.shenmazg6.service.TbProductService;
import com.shenmazg6.mapper.TbProductMapper;
import com.shenmazg6.utils.ResultResponse;
import com.shenmazg6.vo.PageInfoVo;
import com.shenmazg6.vo.TbProductVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author Think
* @description 针对表【tb_product(产品表)】的数据库操作Service实现
* @createDate 2022-12-20 10:54:00
*/
@Service
public class TbProductServiceImpl extends ServiceImpl<TbProductMapper, TbProduct>
    implements TbProductService{

    @Override
    public ResultResponse ListByPage(PageInfoVo pageInfoVo) {

        // 分页查询产品数据
        Page<TbProduct> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        QueryWrapper<TbProduct> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByDesc(TbProduct::getProductId);

        Page<TbProduct> tbProductPage = page(page, wrapper);

        // 数据转换
        Page<TbProductVo> tbProductVoPage = new Page<>();
        BeanUtils.copyProperties(tbProductPage, tbProductVoPage);

        List<TbProductVo> collect = tbProductPage.getRecords().stream().map(item -> {
            TbProductVo tbProductVo = new TbProductVo();
            BeanUtils.copyProperties(item, tbProductVo);
            return tbProductVo;
        }).collect(Collectors.toList());

        tbProductVoPage.setRecords(collect);


        return ResultResponse.SUCCESS(tbProductVoPage);
    }
}




